<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Member extends CI_Controller {
    function __construct() {
        parent::__construct();
        //cek session
        $this->load->model('item', 'item');
        $this->load->model('schedule/users_model', 'message');
        if(!$this->session->userdata('login'))
             redirect(base_url().'home/');
    }
    public function index()
    {   
        $display = array();
        $q_jenis = $this->db->query('select * from category')->result_array();
        $display['kota'] =$this->item->get_dropdown_kota();
        $display['jenis'] =$this->item->get_dropdown_jenis();
        $display['navigation'] = 'DASHBOARD';
        $display['display'] = 'style="display:none;"';
        
        $this->load->view('header', $display);
        $this->load->view('dashboard');
        $this->load->view('footer');
    }
    function add_item(){
        if(empty($_POST['nama']) || empty($_POST['deskripsi']))
            redirect(base_url().'util/error_display');
        $nama = $_POST['nama'];
        $deskripsi = $_POST['deskripsi'];
        $data = array(
            'NAMA' => $nama,
        );
        $this->db->insert('barang', $data);
        redirect(base_url().'dashboard');
    }
    function list_item(){
        $header = $this->loadSearchHeader('style="display:none;"');
        
        $this->load->view('header', $header);
        $this->load->view('dashboard_list_item');
        $this->load->view('footer');
    }
    function rent(){
        $header = array();
        $header['navigation'] = "HOME > SEWA";
        $header = $this->loadSearchHeader('style="display:none;"');
        
        $display = array();
        $kota = $this->item->get_dropdown_kota();
        unset($kota[0]);
        $display['kota'] =$kota;
        $jenis = $this->item->get_dropdown_jenis();
        unset($jenis[0]);
        $display['jenis'] =$jenis;
        
        $this->load->view('header', $header);
        $this->load->view('rent_form', $display);
        $this->load->view('footer');        
    }
    function rent_processing(){
        $name = (!empty($_POST['nama'])) ? $this->input->post('nama') : '';
        $description = (!empty($_POST['deskripsi'])) ? $this->input->post('deskripsi') : '';
        $type = (!empty($_POST['jenis'])) ? $this->input->post('jenis') : '';
        $city = (!empty($_POST['kota'])) ? $this->input->post('kota') : '';
        $ket = (!empty($_POST['keterangan'])) ? $this->input->post('keterangan') : '';
        
        if (empty($name) || empty($description) || empty($type) || empty($city) ||
                empty($ket))
            redirect(base_url() . 'util/error_display');
        $data = array(
            'ID_USER' =>$this->session->userdata('id'),
            'STATUS' => 'belum',
            'NAME' => $name,
            'TIME' => '',
            'ID_CITY' => '',
        );
        $this->db->insert('item', $data);
        
    }
    function profile(){
        $header = $this->loadSearchHeader('style="display:none;"');
        $this->load->view('header', $header);
        $this->load->view('dashboard_profile');
        $this->load->view('footer');        
    }
    
    function to_message(){
        //fetch message info
        $message = $this->message->get_inbox();
        $unread = $this->message->get_total_unread();
        $display['msg'] = $message;
        $display['unread'] = $unread;
        $display['action'] = 0;
        $header = $this->loadSearchHeader('style="display:none;"');
        
        //load views
        $this->load->view('header', $header);
        $this->load->view('message', $display);
        $this->load->view('footer');
    }
    
    function inbox(){
        $this->to_message();
    }
    
    function sent(){
        //fetch message info
        $message = $this->message->get_sent();
        $unread = $this->message->get_total_unread();
        $display['msg'] = $message;
        $display['action'] = 1;
        $display['unread'] = $unread;
        $header = $this->loadSearchHeader('style="display:none;"');
        
        //load views
        $this->load->view('header', $header);
        $this->load->view('message', $display);
        $this->load->view('footer');
    }
    
    function view_msg($id){
        //get message
        $display['msg'] = $this->message->get_message($id);
        $display['action'] = 2;
        $unread = $this->message->get_total_unread();
        $display['unread'] = $unread;
        $display['sent'] = 0;
        $header = $this->loadSearchHeader('style="display:none;"');
        
        //set status as read
        $data['STATUS'] = '0';
        $this->message->mark_as_read($id);
        
        //load views
        $this->load->view('header', $header);
        $this->load->view('message', $display);
        $this->load->view('footer');
    }
    
    function view_sent($id){
        //get message
        $display['msg'] = $this->message->get_message($id);
        $display['action'] = 2;
        $unread = $this->message->get_total_unread();
        $display['unread'] = $unread;
        $display['sent'] = 1;
        $header = $this->loadSearchHeader('style="display:none;"');
        
        //set status as read
        $data['STATUS'] = '0';
        $this->message->mark_as_read($id);
        
        //load views
        $this->load->view('header', $header);
        $this->load->view('message', $display);
        $this->load->view('footer');
    }
    
    function loadSearchHeader($display){
        $header = array();
        $header['navigation'] = "HOME > HASIL PENCARIAN";
        $header['kota'] =$this->item->get_dropdown_kota();
        $header['jenis'] =$this->item->get_dropdown_jenis();
        $header['display'] = $display;
        return $header;
    }
}
